<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="工程编号" prop="projectId">
        <el-input
          v-model="queryParams.projectId"
          clearable
          placeholder="请输入工程编号"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>

    <el-row :gutter="20" class="mb8 list-box">
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadOne"
        >预算书
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadTwo"
        >维修前照片
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadThree"
        >费用划拨单
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadFour"
        >不受理告知书
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadFive"
        >备案表
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadSix"
        >维修后照片
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadSeven"
        >申请使用维修资金公示
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadEight"
        >维修资金使用申请人资格认定证明
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadNine"
        >施工合同
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadTen"
        >维修资金使用查档申请
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadEleven"
        >竣工验收报告
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadTwelve"
        >房屋共用部位共用设施设备
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadThirteen"
        >划拨备案登记表
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadFourteen"
        >现场踏查报告
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadFifteen"
        >维修资金使用流程及准备资料
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadSixteen"
        >维修资金使用业务受理单
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadSeventeen"
        >维修资金使用申请
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadEighteen"
        >委托书
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadNineteen"
        >业主签名表
        </el-button>
      </el-col>
      <el-col :span="6">
        <el-button
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="downloadTwenty"
        >业主会议决议
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

<!--    <el-table v-loading="loading" :data="annexList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="工程编号" prop="projectId"/>
      <el-table-column label="电子档案照片" align="center" prop="electronicImageUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.electronicImageUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="电子档案名称" prop="electronicName"/>
      <el-table-column align="center" label="提交时间" prop="createDate"/>
      <el-table-column align="center" label="提交人" prop="creater"/>
      <el-table-column align="center" label="备注" prop="remark"/>
      <el-table-column align="center" label="文件类别" prop="fileType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.REPAIR004" :value="scope.row.fileType"/>
        </template>
      </el-table-column>
    </el-table>-->

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />
    <!-- 添加或修改获奖信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="200px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="文件类别" prop="fileType">
              <el-select v-model="form.fileType" clearable placeholder="文件类别" size="small">
                <el-option
                  v-for="dict in dict.type.REPAIR004"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="100px" label="上传图片">
              <div class="component-upload-image">
                <el-upload
                  :action="uploadImgUrl"
                  list-type="picture-card"
                  :on-success="handleUploadSuccess"
                  :before-upload="handleBeforeUpload"
                  :limit="limit"
                  :on-error="handleUploadError"
                  :on-exceed="handleExceed"
                  ref="imageUpload"
                  :on-remove="handleDelete"
                  :show-file-list="true"
                  :headers="headers"
                  :file-list="fileList"
                  :on-preview="handlePictureCardPreview"
                  :class="{hide: this.fileList.length >= this.limit}"
                >
                  <i class="el-icon-plus"></i>
                </el-upload>

                <!-- 上传提示 -->
                <div class="el-upload__tip" slot="tip" v-if="showTip">
                  请上传
                  <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
                  <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
                  像素为 <b style="color: #f56c6c">320 x 320 </b>
                  的文件
                </div>

                <el-dialog
                  :visible.sync="dialogVisible"
                  title="预览"
                  width="800"
                  append-to-body
                >
                  <img
                    :src="dialogImageUrl"
                    style="display: block; max-width: 100%; margin: 0 auto"
                  />
                </el-dialog>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

import {downloadOne} from "@/api/repair/annex";
import { getToken } from "@/utils/auth";
import {
  listAnnex,
  downloadEight,
  downloadEighteen,
  downloadEleven,
  downloadFifteen,
  downloadFive,
  downloadFour,
  downloadFourteen,
  downloadNine,
  downloadNineteen,
  downloadSeven,
  downloadSeventeen,
  downloadSix,
  downloadSixteen,
  downloadTen,
  downloadThirteen,
  downloadThree,
  downloadTwelve,
  downloadTwenty,
  downloadTwo,
  addProjectPhoto
} from "../../../api/repair/annex";

export default {
  name: "Annex",
  dicts: ['REPAIR004'],
  props: {
    value: [String, Object, Array],
    limit: {
      type: Number,
      default: 4,
    },
    // 大小限制(MB)
    fileSize: {
      type: Number,
      default: 5,
    },
    fileType: {
      type: Array,
      default: () => ["png", "jpg", "jpeg"],
    },
    // 是否显示提示
    isShowTip: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {

      number: 0,
      uploadList: [],
      dialogImageUrl: "",
      dialogVisible: false,
      hideUpload: false,
      baseUrl: process.env.NODE_ENV === "production" ? "":process.env.VUE_APP_BASE_API,
      uploadImgUrl: (process.env.NODE_ENV === "production" ? "":process.env.VUE_APP_BASE_API) + "/repair/annex/upload", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      fileList: [],



      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 户资金修改表格数据
      annexList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        projectId: null,
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {photoList:null,
        fileType:null,
        projectId:null},
      // 表单校验
      rules: {
        projectId: [
          {required: true, message: "工程编号不能为空", trigger: "blur"}
        ],
        fileType: [
          {required: true, message: "文件类别不能为空", trigger: "blur"}
        ],
      }
    };
  },

  watch: {
    value: {
      handler(val) {
        if (val) {
          // 首先将值转为数组
          const list = Array.isArray(val) ? val : this.value.split(',');
          // 然后将数组转为对象数组
          this.fileList = list.map(item => {
            if (typeof item === "string") {
              if (item.indexOf(this.baseUrl) === -1) {
                item = { name: this.baseUrl + item, url: this.baseUrl + item };
              } else {
                item = { name: item, url: item };
              }
            }
            return item;
          });
        } else {
          this.fileList = [];
          return [];
        }
      },
      deep: true,
      immediate: true
    }
  },
  computed: {
    // 是否显示提示
    showTip() {
      return this.isShowTip && (this.fileType || this.fileSize);
    },
  },



  created() {
    this.getList();
  },
  methods: {
    /** 查询户资金修改列表 */
    getList() {
      this.loading = true;
      listAnnex(this.queryParams).then(response => {
        this.annexList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.houseId = selection.map(item => item.houseId)
      this.projectId = selection.map(item => item.projectId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    downloadOne() {
      return downloadOne(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    },
    downloadTwo() {
      debugger
      return downloadTwo(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    },
    downloadThree() {
      debugger
      return downloadThree(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    },
    downloadFour() {
      return downloadFour(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    },
    downloadFive() {
      return downloadFive(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    }, downloadSix() {
      return downloadSix(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    }, downloadSeven() {
      return downloadSeven(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    }, downloadEight() {
      return downloadEight(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    },
    downloadNine() {
      return downloadNine(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    },
    downloadTen() {
      return downloadTen(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    },
    downloadEleven() {
      return downloadEleven(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    },
    downloadTwelve() {
      return downloadTwelve(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    },
    downloadThirteen() {
      return downloadThirteen(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    },
    downloadFourteen() {
      return downloadFourteen(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    }, downloadFifteen() {
      return downloadFifteen(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    }, downloadSixteen() {
      return downloadSixteen(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    }, downloadSeventeen() {
      return downloadSeventeen(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    }, downloadEighteen() {
      return downloadEighteen(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    }, downloadNineteen() {
      debugger
      return downloadNineteen(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    }, downloadTwenty() {
      return downloadTwenty(this.queryParams).then(response => {
        let baseUrl = "http://" + response.data.addr;
        window.open(baseUrl);
      });
    },



    handleBeforeUpload(file) {
      let isImg = false;
      if (this.fileType.length) {
        let fileExtension = "";
        if (file.name.lastIndexOf(".") > -1) {
          fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
        }
        isImg = this.fileType.some(type => {
          if (file.type.indexOf(type) > -1) return true;
          if (fileExtension && fileExtension.indexOf(type) > -1) return true;
          return false;
        });
      } else {
        isImg = file.type.indexOf("image") > -1;
      }

      if (!isImg) {
        this.$modal.msgError(`文件格式不正确, 请上传${this.fileType.join("/")}图片格式文件!`);
        return false;
      }
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize;
        if (!isLt) {
          this.$modal.msgError(`上传头像图片大小不能超过 ${this.fileSize} MB!`);
          return false;
        }
      }
      this.$modal.loading("正在上传图片，请稍候...");
      this.number++;
    },
    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
    },
    // 上传成功回调
    handleUploadSuccess(res, file) {
      if (res.code === 200) {
        this.uploadList.push({ name: res.fileName ,url: res.fileUrl});
        this.uploadedSuccessfully();
      } else {
        this.number--;
        this.$modal.closeLoading();
        this.$modal.msgError(res.msg);
        this.$refs.imageUpload.handleRemove(file);
        this.uploadedSuccessfully();
      }
    },
    // 删除图片
    handleDelete(file) {
      const findex = this.fileList.map(f => f.name).indexOf(file.name);
      if(findex > -1) {
        this.fileList.splice(findex, 1);
        this.$emit("input", this.listToString(this.fileList));
      }
    },
    // 上传失败
    handleUploadError() {
      this.$modal.msgError("上传图片失败，请重试");
      this.$modal.closeLoading();
    },
    // 上传结束处理
    uploadedSuccessfully() {
      if (this.number > 0 && this.uploadList.length === this.number) {
        this.fileList = this.fileList.concat(this.uploadList);
        this.uploadList = [];
        this.number = 0;
        this.$emit("input", this.listToString(this.fileList));
        this.$modal.closeLoading();
      }
    },
    // 预览
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 对象转成指定字符串分隔
    listToString(list, separator) {
      let strs = "";
      separator = separator || ",";
      for (let i in list) {
        if (list[i].url) {
          strs += list[i].url.replace(this.baseUrl, "") + separator;
        }
      }
      return strs != '' ? strs.substr(0, strs.length - 1) : '';
    },

    submitForm() {
      if(this.fileList.length>0){
        var photolist = [];
        this.fileList.forEach(item=>{
          photolist.push(item.name)
        })
        this.form.photoList = photolist;
        this.form.projectId = this.queryParams.projectId
        addProjectPhoto(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
          this.uploadList = []
          this.fileList = []
          this.resetForm("form");
        });
      }
    },

    handleImport() {
      if(this.queryParams.projectId!=null){
        this.title = "用户导入";
        this.open = true;
      }else{
        this.$modal.msgError("工程编号为空不允许上传！");
      }
    },




  }
};
</script>
<style scoped lang="scss">
// .el-upload--picture-card 控制加号部分
::v-deep.hide .el-upload--picture-card {
  display: none;
}
// 去掉动画效果
::v-deep .el-list-enter-active,
::v-deep .el-list-leave-active {
  transition: all 0s;
}

::v-deep .el-list-enter, .el-list-leave-active {
  opacity: 0;
  transform: translateY(0);
}
.el-col{
  padding-bottom: 20px;
}
.list-box .el-button{
  width: 100%;
  padding: 12px 15px;
  color: #0e9345;
  background: #f8fffb;
  border-color: #dbd2ba;
  border: 1px dashed;
}
.list-box .el-button:hover{
  background: #0e9345;
  color: #fff;
}
</style>
